using UnityEngine;
using System.Collections;

public class bulletFlightPath : MonoBehaviour {
	
	public GameObject bulletPrefab;
	public GameObject gun;
	public Transform origin;
	public GameObject bullet;
	public GameObject character;
	public Animator anim;
	public ParticleSystem gunFlash;
	public ParticleSystem gunJam;
	Vector3 aim = Vector3.zero;
	public bool reloading = false;
	public bool outOfAmmo = false;
	float reloadTimer = 0;
	float reloadDuration = 3;
	int jamPercent = 50;
	bool jamming = false;
	float cycleSpeed = 0.15f; //0.3f;
	float timer = 0;
	public int clipSize = 20;
	public int clipContents = 20;
	
	//audio
	public AudioClip gunfire;
	public AudioClip reload;
	public AudioClip jamSound;
	
	
	
	float bulletSpeed = 4000f;
	public int rangeOfError = 20;
	
	// Use this for initialization
	void Start () {
		clipContents = clipSize;
	}
	
	
	
	// Update is called once per frame
	void Update () {
		if(!reloading && Input.GetKeyDown(KeyCode.Mouse1)){
			reloading = true;
			jamming = jam ();
			if(jamming){
				
				StartCoroutine( playSound(jamSound));
			}else{
				StartCoroutine( playSound(reload));
			}
		}
		
		if(reloading){
			reloadTimer += Time.deltaTime;
			
			
			if (reloadTimer > reloadDuration){
				reloading = false;
				reloadTimer = 0;
				clipContents = clipSize;
				outOfAmmo = false;
				reloadDuration = 3;
				anim.SetBool("Reload", false);
				anim.SetBool("Jam", false);
				gunJam.Stop();
			}
		}
		
		if(!reloading && Input.GetKey(KeyCode.Mouse0) && !reloading){
			if (!outOfAmmo) {
				timer += Time.deltaTime; 
	    		if (timer > cycleSpeed){ 
	       			shoot();
					timer = 0;
	    		}
			}
			else {
				// complain
				GenerateObjects.getPlayer().GetComponent<CharacterManager>().floatMessage( "No Ammo!", Color.red );
			}
		}
		
		// tell hud about bullets
		GenerateObjects.getPlayer().GetComponent<HudScript>().postBullets( clipContents, this.reloadDuration == 8, this.reloading );
	}
	
	
	void shoot (){
		clipContents--;
		StartCoroutine( playSound(gunfire));
		// tell hud about bullets
		GenerateObjects.getPlayer().GetComponent<HudScript>().postBullets( clipContents, this.reloadDuration == 8, this.reloading );
		gunFlash.Play();
		if(clipContents == 0){
			outOfAmmo = true;
			GenerateObjects.getPlayer().GetComponent<CharacterManager>().floatMessage( "No Ammo!", Color.red );
		}
		GameObject bullet = (GameObject) Instantiate(bulletPrefab,gun.transform.position,Quaternion.Euler(0,0,gun.transform.rotation.eulerAngles.z + Random.Range(-rangeOfError,rangeOfError)));
		bullet.rigidbody.AddRelativeForce(Vector3.right * bulletSpeed * Time.deltaTime,ForceMode.VelocityChange);
		Physics.IgnoreCollision(bullet.transform.collider,character.transform.collider);
		
		
	
	}
	bool jam (){
		int j = Random.Range(1,100);
		if(j < jamPercent){
			// TODO tell hud about jam
			anim.SetBool("Jam", true);
			reloadDuration = 8;
			GenerateObjects.getPlayer().GetComponent<CharacterManager>().floatMessage( "* JAM *", Color.yellow );
			gunJam.Play();
			return true;
		}
		else {
			// TODO tell hud about reload
			anim.SetBool("Reload", true);
			// regular reload
			GenerateObjects.getPlayer().GetComponent<CharacterManager>().floatMessage( "Reloading!", Color.blue );
			return false;
		}
	}
	
	public IEnumerator playSound(AudioClip a){
		audio.clip = a;
		audio.Play();
		Debug.Log(a.name);
		if (a.name == "jamSound"){
			yield return new WaitForSeconds(8);
		}else if(a.name == "reload"){
			yield return new WaitForSeconds(4);
		}else{
			yield return new WaitForSeconds(0);
		}
	}
	
	
}
